package com.liziba.demo.slice;

import com.liziba.demo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {

    /** 点击开始标志 */
    private Boolean start = false;
    /** 结束时间 */
    private Long endTime = 0L;
    /** 点击次数统计 */
    private int count = 0;
    /** 文本控件 */
    Text timeText;
    /** 文本控件 */
    Text tipText;
    /** 按钮控件 */
    Button button;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        // 通过id寻找文本控件
        timeText = (Text) this.findComponentById(ResourceTable.Id_time_txt);
        // 通过id寻找文本控件
        tipText = (Text) this.findComponentById(ResourceTable.Id_tip_txt);
        // 通过id寻找按钮控件
        button = (Button) this.findComponentById(ResourceTable.Id_button);
        // 注册监听事件
        button.setClickedListener(this);
    }

    @Override
    public void onClick(Component component) {

        if (component.getId() == ResourceTable.Id_button) {
            if (!start) {
                start = true;
                endTime = System.currentTimeMillis() + 10 * 1000;
                tipText.setText("游戏开始，共计十秒！");
            }
            if (endTime - System.currentTimeMillis() >= 0) {
                timeText.setText(++count + "");
            } else {
                tipText.setText("游戏结束，您10秒内一共点击了！");
            }
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }


}
